package jia.game.combat;

import jia.core.state.CombatState;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import jia.game.GenericFlags;
import org.apache.log4j.Logger;

public class CombatGrid extends TemplateScreen{
    private static final Logger log = Logger.getLogger(CombatGrid.class);
    // player variables
    Image tileSheet;
    Image wallSheet;
    static final int PLAYER_WIDTH = 48;
    static final int PLAYER_HEIGHT = 48;
    static final int TILE_WIDTH = 48;
    static final int TILE_HEIGHT = 48;
    static final int FLOOR_TILE = 0;
    static final int MAP_WIDTH = 50;
    static final int MAP_HEIGHT = 50;
    protected ArrayList combatants;
    protected CombatSpace[][] combatSpaces = new CombatSpace[MAP_WIDTH][MAP_HEIGHT];
    protected int charStateX;
    protected int charStateY;
    protected int newSpaceX;
    protected int newSpaceY;
    protected boolean isAttacking = false;
    
    public CombatGrid(ArrayList combatants){
        try {
            // load the tiles sheet
            tileSheet = ImageIO.read(new File("resources/images/sheet48.jpg"));
            wallSheet = ImageIO.read(new File("resources/images/sheet49.jpg"));
        } catch(IOException e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        
        // place the walls around the edge...
        // horizontal walls
        for(int i = 0; i < MAP_WIDTH; i++) {
            combatSpaces[i][0] = new CombatSpace(false, tileSheet, i, 0, FLOOR_TILE, Color.black);
            combatSpaces[i][MAP_HEIGHT - 1] = new CombatSpace(true, tileSheet, i, MAP_HEIGHT - 1, FLOOR_TILE, Color.black);
        }
        
        // vertical walls
        for(int i = 0; i < MAP_HEIGHT; i++) {
            combatSpaces[0][i] = new CombatSpace(false, tileSheet, 0, i, FLOOR_TILE, Color.black);
            combatSpaces[MAP_WIDTH - 1][i] = new CombatSpace(true, tileSheet, MAP_WIDTH - 1, i, FLOOR_TILE, Color.black);
        }
        
        // fill in middle with floor tiles...
        for(int i = 1; i < MAP_WIDTH - 1; i++) {
            for(int j = 1; j < MAP_HEIGHT - 1; j++) {
                combatSpaces[i][j] = new CombatSpace(true, tileSheet, i, j, FLOOR_TILE, Color.black);
            }
        }
        
        //loop through the combatant list and place circles on a grid for each combatant
        for(Object combatant1 : combatants) {
            CombatState combatant = (CombatState) combatant1;
            charStateX = new Double(combatant.getX()).intValue();
            charStateY = new Double(combatant.getY()).intValue();
            combatSpaces[charStateX][charStateY].setCurrentCharacter(combatant);
            combatSpaces[charStateX][charStateY].setOccupied(true);
        }
        
        //loop through a list of blocked squares, setting passable to be false
        combatSpaces[2][6].setPassable(false);
        combatSpaces[3][6].setPassable(false);
        combatSpaces[4][6].setPassable(false);
        combatSpaces[5][6].setPassable(false);
        combatSpaces[6][6].setPassable(false);
        combatSpaces[7][6].setPassable(false);
        combatSpaces[8][6].setPassable(false);
        combatSpaces[9][6].setPassable(false);
        combatSpaces[10][6].setPassable(false);
        combatSpaces[11][6].setPassable(false);
        combatSpaces[12][6].setPassable(false);
        combatSpaces[13][6].setPassable(false);
        
    }
    
    /**
     * @param characterState
     */
    public void movePlayer(CombatState characterState, Point newPoint){
        //TODO: takes the current xy coordinate  on the combat state
        charStateX = new Double(characterState.getX()).intValue();
        charStateY = new Double(characterState.getY()).intValue();
        newSpaceX = new Double(newPoint.getX()).intValue();
        newSpaceY = new Double(newPoint.getY()).intValue();
        
        //clear the current space
        combatSpaces[charStateX][charStateY].setCurrentCharacter(null);
        combatSpaces[charStateX][charStateY].setOccupied(false);
        
        //Set the new spaced to occupied and assign the character state to it.
        combatSpaces[newSpaceX][newSpaceY].setCurrentCharacter(characterState);
        combatSpaces[newSpaceX][newSpaceY].setOccupied(true);
    }
    
    public boolean enemyOccupiedSquare(Point thisPoint){
        newSpaceX = new Double(thisPoint.getX()).intValue();
        newSpaceY = new Double(thisPoint.getY()).intValue();
        CombatSpace thisSpace = combatSpaces[newSpaceX][newSpaceY];
        if(thisSpace.isOccupied() && thisSpace.getCurrentCharacter().isEnemy()) {
            return true;
        }
        return false;
    }
    
    public boolean friendlyOccupiedSquare(Point thisPoint){
        newSpaceX = new Double(thisPoint.getX()).intValue();
        newSpaceY = new Double(thisPoint.getY()).intValue();
        CombatSpace thisSpace = combatSpaces[newSpaceX][newSpaceY];
        if(thisSpace.isOccupied() && !thisSpace.getCurrentCharacter().isEnemy()) {
            return true;
        }
        return false;
    }
    
    public void clearDeadPlayer(CombatState characterState){
        charStateX = new Double(characterState.getX()).intValue();
        charStateY = new Double(characterState.getY()).intValue();
        combatSpaces[charStateX][charStateY].setOccupied(false);
        combatSpaces[charStateX][charStateY].setCurrentCharacter(null);
        if (GenericFlags.debugMessages){
            log.debug("Cleared space " +characterState.getX()+" / " +characterState.getY()+ " for " + characterState.getCharacter().getCharacterName());
        }
    }
    
    public void setAttackState(){
        this.isAttacking = true;
    }
    
    public void setDefaultState(){
        this.isAttacking = false;
    }
    
    /**
     * @param g
     */
    public void render(Graphics g){
        int srcX;
        int tileX = 0;
        int tileY;
        for(int x = 0; x < CombatGlobals.DISPLAY_WIDTH; x += TILE_WIDTH) {
            tileY = 0;
            for(int y = 0; y < CombatGlobals.DISPLAY_HEIGHT; y += TILE_HEIGHT) {
                srcX = combatSpaces[tileX][tileY].getTileType() * TILE_WIDTH;
                
                //Draw Ground Tiles
                g.drawImage(tileSheet, x, y, x + TILE_WIDTH, y + TILE_HEIGHT, srcX, 0, srcX + TILE_WIDTH, TILE_HEIGHT, null);
                if (!combatSpaces[tileX][tileY].isPassable()){
                    g.drawImage(wallSheet, x, y, x + TILE_WIDTH, y + TILE_HEIGHT, srcX, 0, srcX + TILE_WIDTH, TILE_HEIGHT, null);
                }
                
                if(combatSpaces[tileX][tileY].isOccupied()) {
                    if(isAttacking) {
                        //Draw character Sprites
                        g.drawImage(combatSpaces[tileX][tileY].getCurrentCharacter().getAttackSprite(), x, y, x + TILE_WIDTH, y + TILE_HEIGHT, srcX, 0, srcX + TILE_WIDTH, TILE_HEIGHT, null);
                    } else {
                        //Draw character Sprites
                        g.drawImage(combatSpaces[tileX][tileY].getCurrentCharacter().getAttackSprite(), x, y, x + TILE_WIDTH, y + TILE_HEIGHT, srcX, 0, srcX + TILE_WIDTH, TILE_HEIGHT, null);
                    }
                }
                tileY++;
            }
            tileX++;
        }
    }
}